/**
 * 
 */
package com.kgcx.schedule.rabbitmq.consumer;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.kgcx.common.client.order.pojo.Order;
import com.kgcx.common.rabbitmq.consts.QueueConst;
import com.kgcx.core.utils.JsonUtil;
import com.kgcx.schedule.domain.ScheduleRecordDomain;
import com.kgcx.schedule.rabbitmq.producer.OrderScheduledProducer;
import com.kgcx.schedule.service.DriverStateService;
import com.kgcx.schedule.service.ScheduleRecordService;

/**
 * @author 袁进勇
 *
 */
@Component
public class OrderChangedConsumer {
    private static final Logger LOG = LoggerFactory.getLogger(OrderChangedConsumer.class);

    @Resource
    private ScheduleRecordService scheduleRecordService;
    @Resource
    private DriverStateService driverStateService;
    @Autowired
    private OrderScheduledProducer orderScheduledProducer;

    @RabbitListener(queues = QueueConst.SCHEDULE_ORDER_CANCEL, errorHandler = "custRabbitListenerErrorHandler")
    @Transactional(rollbackFor = Exception.class)
    public void orderCancelled(String orderJson) {
        LOG.info("收到订单取消的MQ消息。\n" + orderJson);
        Order order = JsonUtil.toObject(orderJson, Order.class);
        scheduleRecordService.cancelRecord(order.getOrderNo());
        if (DriverStateService.SWITCH) {
            if (null != order.getDriverId()) {
                driverStateService.orderCancelled(order.getDriverId());
            }
        }
    }

    @RabbitListener(queues = QueueConst.SCHEDULE_ORDER_START, errorHandler = "custRabbitListenerErrorHandler")
    public void orderStarted(String json) {
        if (!DriverStateService.SWITCH) {
            return;
        }
        LOG.info("收到行程开始的MQ消息。\n" + json);

        Order order = JSONObject.parseObject(json, Order.class);
        if (null != order.getDriverId()) {
            driverStateService.orderStarted(order.getDriverId());
        }
    }

    @RabbitListener(queues = QueueConst.SCHEDULE_ORDER_FINISH, errorHandler = "custRabbitListenerErrorHandler")
    public void orderFinished(String orderJson) {
        LOG.info("收到订单行程结束的MQ消息。\n" + orderJson);
        Order order = JsonUtil.toObject(orderJson, Order.class);
        ScheduleRecordDomain record = scheduleRecordService.finishRecord(order.getOrderNo(), order.getDestTime());
        if (record != null) {
            if (DriverStateService.SWITCH) {
                driverStateService.orderFinished(record.getDriver_id());
            } else {
                orderScheduledProducer.unlockDriver(record);
            }
        }
    }

    @RabbitListener(queues = QueueConst.SCHEDULE_PUSH_BILL, errorHandler = "custRabbitListenerErrorHandler")
    @Transactional(rollbackFor = Exception.class)
    public void pushBill(String orderJson) {
        LOG.info("收到推送账单的MQ消息。\n" + orderJson);
        Order order = JsonUtil.toObject(orderJson, Order.class);
        scheduleRecordService.archiveScheduleRecord(order.getOrderNo());
        if (DriverStateService.SWITCH) {
            driverStateService.pushBill(order.getDriverId());
        }
    }
}
